package com.zygh.policeasset.controller;

import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.pojo.ProjectSupervisor;
import com.zygh.policeasset.service.ProjectSupervisorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RequestMapping({"/projectSupervisor"})
@Api(tags = {"项目监理信息管理"})
@RestController
public class ProjectSupervisorController {

    @Autowired
    ProjectSupervisorService projectSupervisorService;

    @RequestMapping(value = {"/"}, method = {RequestMethod.GET})
    @ApiOperation("获取列表")
    public Object findAll(@RequestParam(defaultValue = "1", value = "pageNum", required = false) Integer pageNum,
                          @RequestParam(defaultValue = "10", value = "pageSize", required = false) Integer pageSize,
                          @RequestParam(value = "status", required = false) Integer status,
                          @RequestParam(value = "projectNo", required = false) String projectNo,
                          @RequestParam(required = false) String time1,
                          @RequestParam(required = false) String time2,
                          @RequestHeader("findAll") boolean findAll,
                          @RequestHeader("userId") Integer userId) {

        if (findAll) {
            return new CommonResult().success(projectSupervisorService.findAll(pageNum, pageSize,status,projectNo,time1,time2,null));
        }
        return new CommonResult().success(projectSupervisorService.findAll(pageNum, pageSize,status,projectNo,time1,time2,userId));
    }

    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST})
    @ApiOperation("添加")
    public Object add(@RequestBody ProjectSupervisor projectSupervisor) {
        if (this.projectSupervisorService.add(projectSupervisor) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加错误");
    }

    @RequestMapping(value = {"/update"}, method = {RequestMethod.POST})
    @ApiOperation("更新")
    public Object update(@RequestBody ProjectSupervisor projectSupervisor) {
        if (projectSupervisorService.update(projectSupervisor) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("更新错误");
    }

    @DeleteMapping({"/delete/{id}"})
    @ApiOperation("删除")
    public CommonResult delete(@PathVariable Integer id) {

        if (projectSupervisorService.delete(id) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除错误");
    }

    @GetMapping({"/findById"})
    @ApiOperation("通过id查询")
    public CommonResult findById(Integer id) {
        return new CommonResult().success(projectSupervisorService.findById(id));
    }
}